package com.study.handler;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

import java.util.Date;

/**
 * @Description: TODO
 * @Author: csc
 * @Date: 2024/8/7 11:02
 * @Version: 1.0
 **/
public class MyDateConverter  implements Converter<Date> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Date.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.DATE;
    }

    /**
     * 这里读的时候会调用
     *
     * @param context
     * @return
     */
    @Override
    public Date convertToJavaData(ReadConverterContext<?> context) {

        return DateUtil.parse(context.getReadCellData().getStringValue());
    }

    /**
     * 这里是写的时候会调用 不用管
     * @return
     */
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Date> context) {
        Date value = context.getValue();
        String format = DateUtil.format(value, "yyyy-MM-dd HH:mm:ss");
        System.out.println(format);
        return new WriteCellData<>(format);
    }

}
